package com.example.smartcity1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.HospitalListBean;

import java.util.List;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class HospitalListAdapter extends ArrayAdapter<HospitalListBean.RowsDTO> {


    public HospitalListAdapter(@NonNull Context context, int resource, @NonNull List<HospitalListBean.RowsDTO> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_hospital_list, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.imageView);
            viewHolder.title = convertView.findViewById(R.id.title);
            viewHolder.star = convertView.findViewById(R.id.star);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        HospitalListBean.RowsDTO item = getItem(position);
        Glide.with(getContext()).load(IP + item.getImgUrl())
                .centerCrop()
                .into(viewHolder.imageView);
        viewHolder.title.setText(item.getHospitalName());
        viewHolder.star.setText(String.format("星级：%s星", item.getLevel()));
        return convertView;
    }


    private class ViewHolder {
        ImageView imageView;
        TextView title;
        TextView star;
    }
}
